package com.yihu.wlyy.web.doctor.team;

import java.util.List;

import io.swagger.annotations.Api;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yihu.wlyy.entity.doctor.Doctor;
import com.yihu.wlyy.service.app.account.DoctorInfoService;
import com.yihu.wlyy.service.app.team.DrHealthTeamService;
import com.yihu.wlyy.util.CommonUtil;
import com.yihu.wlyy.web.BaseController;

@Controller
@RequestMapping(value = "/doctor/team")
@Api(description = "医生健康(医疗)团队")
public class DrHealthTeamController extends BaseController {

    @Autowired
    private DoctorInfoService doctorInfoService;

    @Autowired
    private DrHealthTeamService drHealthTeamService;

    /**
     * 查询医生所有的团队數目
     *
     * @return
     */
    @RequestMapping(value = "teamListCount", method = RequestMethod.GET)
    @ResponseBody
    public String teamListCount() {
        try {
            JSONObject json = new JSONObject();
            String doctorCode = super.getUID();
            Integer count = drHealthTeamService.teamListCountByDoctor(doctorCode);
            json.put("count", count);
            return write(200, "医生信息查询成功！", "data", json);
        } catch (Exception e) {
            error(e);
            return error(-1, "查询失败！");
        }
    }

    /**
     * 查询醫生所有的团队
     *
     * @return
     */
    @RequestMapping(value = "teamList", method = RequestMethod.GET)
    @ResponseBody
    public String teamList() {
        try {
            JSONObject json = new JSONObject();
            String doctorCode = super.getUID();
            json.put("count", drHealthTeamService.teamListByDoctorCode(doctorCode));
            return write(200, "医生信息查询成功！", "data", json);
        } catch (Exception e) {
            error(e);
            return error(-1, "查询失败！");
        }
    }

    /**
     * 查询指定医生的基本信息接口
     *
     * @param doctor 医生标识
     * @return
     */
    @RequestMapping(value = "info", method = RequestMethod.GET)
    @ResponseBody
    public String info(String doctor) {
        try {
            Doctor temp = doctorInfoService.findDoctorByCode(doctor);
            if (temp != null) {
                JSONObject json = new JSONObject();
                // 设置医生姓名
                json.put("name", temp.getName());
                // 设置医生头像
                json.put("photo", CommonUtil.getPhoneUrl(temp.getPhoto()));
                // 设置医生性别
                json.put("sex", temp.getSex());
                // 设置省名称
                json.put("provinceName", temp.getProvinceName());
                // 设置城市名称
                json.put("cityName", temp.getCityName());
                // 设置医院名称
                json.put("hospitalName", temp.getHosptialName());
                // 设置部门名称
                json.put("deptName", temp.getDeptName());
                // 设置职称
                json.put("jobName", temp.getJobName());
                // 设置医生专长
                json.put("expertise", temp.getExpertise());
                // 设置医生简介
                json.put("introduce", temp.getIntroduce());
                // 二维码地址
                json.put("qrcode", temp.getQrcode());
                return write(200, "医生信息查询成功！", "data", json);
            } else {
                return error(-1, "医生信息查询失败！");
            }
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "患者信息查询失败！");
        }
    }


    /**
     * 查询团队中健康管理师或全科医生
     *
     * @param type
     * @return
     */
    @RequestMapping(value = "doctors", method = RequestMethod.GET)
    @ResponseBody
    public String getTeamDoctors(
            String type,
            Integer page,
            Integer pageSize) {
        try {
            List<Object> doctors = drHealthTeamService.findTeamDoctors(getUID(), type, page, pageSize);

            return write(200, "查询成功！", "data", new JSONArray(doctors));
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "查询失败！");
        }
    }

    /**
     * 查询医生团队中的某个类别医生
     *
     * @param type
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "teamdoctors", method = RequestMethod.GET)
    @ResponseBody
    public String getTeamDoctorsByType(String type, Integer page,
                                       Integer pageSize) {
        try {
            List<Object> doctors = drHealthTeamService.findTeamDoctorsByType(getUID(), type, page, pageSize);
            return write(200, "查询成功！", "data", new JSONArray(doctors));
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "查询失败！");
        }
    }
}
